package com.spzx.report.vectorTools;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dxt
 * @create 2025-06-10-17:25
 */

public class SpzxEmbeddingTools {
    public static Embedding getEmbedding(String text) {
        try {
            EmbeddingModel embeddingModel = OpenAiEmbeddingModel
                    .builder()
                    .apiKey(SpzxEmbeddingConst.ALI_API_KEY)
                    .baseUrl(SpzxEmbeddingConst.ALI_EMBEDDINGS_BASE_URL)
                    .modelName(SpzxEmbeddingConst.ALI_EMBEDDINGS_MODEL)
                    .build();

            Response<Embedding> response = embeddingModel.embed(text);

            // 直接获取内容，可能会抛出异常
            return response.content();
        } catch (Exception e) {
            throw new RuntimeException("生成文本嵌入时发生错误", e);
        }
    }
}
